package control;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManage111
{
	private static ConnectionManage111 instance = null;

	private static final String USERNAME = "root";
	private static final String PASSWORD = "31387650";
	private static final String H_CONN_STRING =
			"jdbc:hsqldb:data/explorecalifornia";
	private static final String M_CONN_STRING =
			"jdbc:mysql://localhost:3306/vcp?zeroDateTimeBehavior=convertToNull";

	private DBType dbType = DBType.MYSQL;
	private Connection conn = null;
	private ConnectionManage111() {
		
	}

	public static ConnectionManage111 getInstance() {
		if (instance == null) {
			instance = new ConnectionManage111();
		}
		return instance;
	}

	public void setDBType(DBType dbType) {
		this.dbType = dbType;
	}

	private boolean openConnection()
	{
		try {
			switch (dbType) {

			case MYSQL:
				conn = DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);
				return true;

			case HSQLDB:
				conn = DriverManager.getConnection(H_CONN_STRING, USERNAME, PASSWORD);
				return true;

			default: 
				return false;
			}
		}
		catch (SQLException e) {
			System.err.println(e+" ConnectionManage user="+USERNAME+"  "+ "pass="+ PASSWORD);
			return false;
		}

	}

	public Connection getConnection()
	{
		if (conn == null) {
			if (openConnection()) {
				//System.out.println("Connection opened");
				return conn;
			} else {
				return null;
			}
		}
		return conn;
	}

	public void close() {
		System.out.println("Closing connection");
		try {
			conn.close();
			conn = null;
		} catch (Exception e) {
		}
	}

}